package com.example.foo.api.web.wechat;

import com.example.support.wechat.boot.WechatProperties;
import com.example.support.wechat.general.WechatGeneralJsSdkConfigResponse;
import com.example.support.wechat.general.WechatGeneralSdkClient;

import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

@org.springframework.web.bind.annotation.RestController
public class WechatMpAction {

	private final WechatProperties wechatProperties;
	private final WechatGeneralSdkClient wechatGeneralSdkClient;

	public WechatMpAction(WechatProperties wechatProperties) {
		this.wechatProperties = wechatProperties;
		this.wechatGeneralSdkClient = new WechatGeneralSdkClient();
	}

	@PostMapping(value = "/wechat/mp/js_sdk_config")
	public WechatGeneralJsSdkConfigResponse js_sdk_config(@RequestParam("appId") String appId, @RequestParam("url") String url) {
		if (StringUtils.isBlank(appId)) {
			throw new IllegalArgumentException("appId must not be null");
		}
		if (StringUtils.isBlank(url)) {
			throw new IllegalArgumentException("url must not be null");
		}

		WechatProperties.Wechat wechat = this.wechatProperties.getMappings().get(appId);

		return wechatGeneralSdkClient.jsSdkConfig(wechat.getMp().getAppId(), wechat.getMp().getAppSecret(), url);
	}

}
